import java.io.*;
import java.util.*;

/**
 *
 * @author TQNINH
 */
class ArizonaBook {
    
    public static void main(String[] arg){
        Map mapIndex = new TreeMap();
        Set setLine;
        int lineNo;
        String word;
        String wordLine;
        try {
            FileReader fr = new FileReader("Ass_5_NinhTQ.java");
            LineNumberReader reader = new LineNumberReader(fr);
            while ((wordLine = reader.readLine()) != null) {
                lineNo = reader.getLineNumber();
                StringTokenizer token = new StringTokenizer(wordLine," ,.!?*;+={}()[]\\/\'\"\t");
                while (token.hasMoreTokens()) {
                    word = token.nextToken().toLowerCase();
                    if (word.length() >= 3 && !word.equalsIgnoreCase("the")) {
                        if (mapIndex.containsKey(word)) {
                            setLine = (Set) mapIndex.get(word);
                            setLine.add(lineNo);
                        } else {
                            setLine = new TreeSet();
                            setLine.add(lineNo);
                            mapIndex.put(word,setLine);
                        }
                    }
                }
            }
            FileWriter fw = new FileWriter("ArizonaBookIndex.txt");
            PrintWriter pw = new PrintWriter(fw);
            Set keys = mapIndex.keySet();
            Iterator it = keys.iterator();
            while(it.hasNext()) {
                word = (String)it.next();
                setLine = (Set) mapIndex.get(word);
                pw.println(word + "\t\t\t" + setLine.toString());
            }
            pw.close();
            fw.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
        Runtime r = Runtime.getRuntime();
        try {
            r.exec("notepad.exe ArizonaBookIndex.txt");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
